package rickyGame.net.handler
{
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.graphic.DefaultScence;
	import rickyGame.game.graphic.PlayerGraphic;
	import rickyGame.ui.login.LoginView;

	public class LoginHandler
	{
		public function LoginHandler()
		{
			
		}
		
		/**
		 * 创建英雄
		 */
		public function showLogin(msg:Object):void
		{
			trace("开始创建英雄");
			var p:LoginView=new LoginView();
			p.initialize();
			p.addEventListener("createSure",onCreated);
			GameState.inst.uiLayer.addChild(p);
		}
		
		private function onCreated(e:Event):void
		{
			var p:LoginView=e.target as LoginView;
			var hreoName:String=p.getHeroName();
			var unit:String=p.choose.data.unitID;

			var obj:Object=new Object();
			obj["cmd"]="CreateHero";
			obj["unitID"]=unit;
			obj["heroName"]=hreoName;
			
			sendMTS(obj);
		}
		
		/**
		 * 添加英雄
		 */
		public function addHero(msg:Object):void
		{
			var sHero:Object=msg["hero"];
			var hero:Player=new Player();
			PlayerGraphic(hero.graphic).defaultScene=DefaultScence.deafultAnimScene;
			hero.load(sHero.unit);
			hero.wear();
			hero.direction=3;
			hero.playAnimAct("stand");
			hero.loadProperty(100);
			hero.gridX=sHero.x;
			hero.gridY=sHero.y;
			hero.name=sHero.name;
			GameState.inst.addGameObject(hero)

		}
		
		/**
		 * 获取英雄基本信息
		 */
		public function setHero(msg:Object):void
		{
			var sHero:Object=msg["hero"];
			var hero:Player=new Player();
			PlayerGraphic(hero.graphic).defaultScene=DefaultScence.deafultAnimScene;
			hero.load(sHero.unit);
			hero.wear();
			hero.direction=3;
			hero.playAnimAct("stand");
			hero.loadProperty(100);

			hero.name=sHero.name;
			JDebug.traceObj(sHero);
			GameState.inst.setMe(hero);
			sendMTS({"cmd":"Scene","type":0,"sceneID":3});
		}
		
		/**
		 * 开始加载地图
		 */
		public function loadMap(msg:Object):void
		{
			JDebug.trace("开始加载地图"+msg.mapURL);

			if(GameState.inst.uiLayer.numChildren>=1)
				GameState.inst.uiLayer.removeChildAt(0);

			GameState.inst.sceneManager.clearScene();
			var URL:String=String(msg.mapURL).substr(1);
			GameState.inst.sceneManager.loadScene(URL);

		}
		
		public function enter(msg:Object):void
		{
			var x:int=msg["x"];
			var y:int=msg["y"];
			GameState.inst.me.active=true;
			GameState.inst.me.gridX=x;
			GameState.inst.me.gridY=y;
			GameState.inst.addGameObject(GameState.inst.me);
			GameState.inst.me.playAnimAct(GameState.inst.me.data.animProps.split(",")[0]);
			GameState.inst.sceneManager.addToStage();
			GameState.inst.sceneManager.currScene.camera.offMoving(GameState.inst.me.graphic.x,GameState.inst.me.graphic.y);
			GameState.inst.isRunning=true;
			JDebug.trace("进入地图"+msg.hero);
			GameState.inst.me.calcMorton();
			GameState.inst.onMovingFun(GameState.inst.me);
			GameState.inst.stepFunction(GameState.inst.me);
			
		}
		
	}
}